local hx__shizhan = fk.CreateSkill {
  name = "hx__shizhan",
  tags = { Skill.Switch },
}

hx__shizhan:addEffect("active", {
  anim_type = "switch",
  can_use = function(self, player)
    return player.phase == Player.Play and player:usedSkillTimes(hx__shizhan.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  target_filter = function(self, player, to_select, selected)
    if player:getSwitchSkillState(hx__shizhan.name, false) == fk.SwitchYang then
      return #selected == 0 and to_select ~= player
    else
      return #selected == 0 and to_select == player
    end
  end,
  target_num = 1,
  prompt = function(self, player)
		if player:getSwitchSkillState(hx__shizhan.name, false) == fk.SwitchYang then
      return "#hx__shizhan-yang"
    else
      return "#hx__shizhan-yin"
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if player:getSwitchSkillState(hx__shizhan.name, true) == fk.SwitchYang then
      room:loseHp(player, 1, hx__shizhan.name)
      if player.dead then return end
      if target:isNude() then return end
      local id1 = room:askToChooseCard(player,{
        target = target,
        flag = "he",
        skill_name = hx__shizhan.name,
        prompt = "#hx__shizhan-card1",
      })
      room:throwCard({id1}, hx__shizhan.name, target, player)
      local target2 = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player),
        min_num = 1,
        max_num = 1,
        skill_name = hx__shizhan.name,
        prompt = "#hx__shizhan-choose",
        cancelable = true,
      })
      if #target2 ~= 0 then
        if target2[1]:isNude() then return end
        local id2 = room:askToChooseCard(player,{
          target = target2[1],
          flag = "he",
          skill_name = hx__shizhan.name,
          prompt = "#hx__shizhan-card2",
        })
        room:throwCard({id2}, hx__shizhan.name, target2[1], player)
      end
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = hx__shizhan.name
      })
      player:drawCards(2, hx__shizhan.name)
    end
    local x = player.maxHp - player.hp
    local y = player.hp
     player:drawCards(x, hx__shizhan.name)
     room:addPlayerMark(player, "@hx__shizhan-turn", y)
  end,
})

hx__shizhan:addEffect("targetmod", {
  mute = true,
  is_delay_effect = true,
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase and player.phase == Player.Play and player:getMark("@hx__shizhan-turn") > 0 then
      return player:getMark("@hx__shizhan-turn") - 1
    end
    return 0
  end,
})

return hx__shizhan